﻿using AutoMapper;
using MediatR;
using Purchase.ApiS.AllCation.Command.Supplier;
using Purchase.Domian.Model;
using Purchase.ErrorCode;
using Purchase.Infrastructrue.Implenment;

namespace Purchase.ApiS.AllCation.Hanlder.Supplier
{
    public class AddPurchaseContractModelCommandHanlder : IRequestHandler<AddPurchaseContractModelCommand, ApiResult<int>>
    {
        DatabaseContext db;
        public readonly IMapper mapper;
        public readonly ILogger<AddPurchaseContractModelCommandHanlder> logger;

        public AddPurchaseContractModelCommandHanlder(DatabaseContext db, IMapper mapper, ILogger<AddPurchaseContractModelCommandHanlder> logger)
        {
            this.db = db;
            this.mapper = mapper;
            this.logger = logger;
        }
        /// <summary>
        /// 新增采购合同信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>

        public Task<ApiResult<int>> Handle(AddPurchaseContractModelCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var model = mapper.Map<PurchaseContractModel>(request);
                int r = db.SqlSugar.Insertable(model).ExecuteCommand();
                if (r > 0)
                {
                    result.Data = r;
                    result.Msg = "添加供应商信息成功";
                    result.Code = ApiEume.Success;
                }
                else
                {
                    result.Data = r;
                    result.Msg = "添加供应商信息失败";
                    result.Code = ApiEume.Error;
                }
            }
            catch (Exception ex)
            {

            }
            return Task.FromResult(result);
        }
    }
}
